package com.bestpay.security.support.startup;

import com.google.common.util.concurrent.AbstractIdleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * User: Terence
 * Date: 2014/6/6
 * Time: 20:56
 */
public class SecurityBootstrap extends AbstractIdleService {
    /*private SecurityBootstrap() {
    }*/

    private final static Logger LOG = LoggerFactory.getLogger(SecurityBootstrap.class);

    private ClassPathXmlApplicationContext context;

    public static void main(String[] args) {
        SecurityBootstrap bootstrap = new SecurityBootstrap();
        bootstrap.startAsync();
        try {
            Object lock = new Object();
            synchronized (lock) {
                while (true) {
                    lock.wait();
                }
            }
        } catch (InterruptedException ex) {
            LOG.error("ignore interruption");
        }
    }

    /**
     * Start the service.
     */
    @Override
    protected void startUp() throws Exception {
        context = new ClassPathXmlApplicationContext(new String[]{"spring/security-spring-context.xml"});
        context.start();
        context.registerShutdownHook();
        LOG.info("security service started successfully");
    }

    /**
     * Stop the service.
     */
    @Override
    protected void shutDown() throws Exception {
        context.stop();
        LOG.info("security service stopped successfully");
    }
}
